package com.bailong.administrator.ausedcar.ui.search;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;

import com.bailong.administrator.ausedcar.R;
import com.bailong.administrator.ausedcar.bean.BuyCarsBean;
import com.bailong.administrator.ausedcar.ui.buycarsfragment.BuyCarsAdapter;
import com.bailong.administrator.ausedcar.ui.buycarsfragment.BuyCarsSingleAdapter;
import com.bailong.administrator.ausedcar.widget.MyGridView;
import com.bailong.administrator.ausedcar.widget.MyListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class SearchActivity extends AppCompatActivity implements SearchContract.View{

    @Bind(R.id.search_spinner_money)
    Spinner searchSpinnerMoney;
    @Bind(R.id.search_spinner_brand)
    Spinner searchSpinnerBrand;
    @Bind(R.id.search_spinner_sort)
    Spinner searchSpinnerSort;
    @Bind(R.id.search_imagebtn)
    ImageButton searchImagebtn;
    @Bind(R.id.search_iv)
    ImageView searchIv;
    @Bind(R.id.search_gridview)
    MyGridView searchGridview;
    @Bind(R.id.search_listview)
    MyListView searchListview;

    private Activity activity=this;

    private List<BuyCarsBean.DataBean.ListBean> lists = new ArrayList<>();
    private List<BuyCarsBean.DataBean.ListBean> list = new ArrayList<>();

    private BuyCarsAdapter adapter;
    private BuyCarsSingleAdapter singleadapter;
    private boolean flag;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        ButterKnife.bind(this);
        String  serieid = getIntent().getStringExtra("serieid");
        SearchPresreter searchPresreter =new SearchPresreter(this,serieid);
        searchPresreter.loadBuyCarsBean();
    }

    @Override
    public void onResponse(final BuyCarsBean buyCarsBean) {
        setAdapter(buyCarsBean);

    }

    @Override
    public void onFailure(String s) {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        finish();
        return super.onKeyDown(keyCode, event);
    }
    //加载适配器
    private void setAdapter(final BuyCarsBean buyCarsBean) {


        adapter = new BuyCarsAdapter(list, activity);
        singleadapter = new BuyCarsSingleAdapter(list, activity);
        searchListview.setAdapter(singleadapter);
        searchListview.setEmptyView(searchGridview);
        searchGridview.setAdapter(adapter);
        searchGridview.setEmptyView(searchListview);
        adapter.reloadData(buyCarsBean.getData().getList());
        searchImagebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (flag) {

                    searchImagebtn.setBackgroundResource(R.drawable.web);
                    flag = false;
                    adapter.reloadData(lists);


                    singleadapter.reloadData(buyCarsBean.getData().getList());
                } else {

                    searchImagebtn.setBackgroundResource(R.drawable.webb);
                    flag = true;

                    singleadapter.reloadData(lists);
                    adapter.reloadData(buyCarsBean.getData().getList());
                }

            }
        });

    }
}
